﻿var _countdownTimer;
var _popupTimer;
var _timeLeft;

    var hidePopup = function() {
        $("table[id*='SessionExpiredTable']").css("display", "none");
        $("table[id*='SessionExpiredTable']").css("visibility", "hidden");
        $('#timeout_box').fadeOut("slow");
        $("#timeout_modal_overlay").removeClass("modalPopup");
        $("#timeout_modal_overlay").css("z-index", "0");
    }

    var showPopup = function () {
        $("table[id*='SessionExpiredTable']").css("display", "block");
        $("table[id*='SessionExpiredTable']").css("visibility", "visible");
        $('#timeout_box').fadeIn("slow");
        $("#timeout_modal_overlay").addClass("modalPopup");
        $("#timeout_modal_overlay").css("z-index", "1000");

        clearCountDown(); // clear previous countdown.
        _timeLeft = 15; // used for new timer countdown.
        _countdownTimer = window.setInterval(showCountDown, 1000); // display countdown every second.
    }

    var extendSession = function () {
        stopTimers();
        hidePopup();
        clientCallServer();
        schedulePopup();
    }

    var showCountDown = function () {
        $("#expiredTime").html(_timeLeft.toString());
        if (_timeLeft > 0) {
            _timeLeft--;
        }
        else {
            ShowExpired();
        }
    }

    var clearCountDown = function () {
        $("#expiredTime").html("");
    }

    var schedulePopup = function () {
        var timeOut = $("input:hidden[id*='TimeOutHiddenField']").val();
        stopTimers();

        // show appropriate popup after the session times out.
        _popupTimer = window.setTimeout(showPopup, (timeOut * 60) * 1000);           
    }

    var stopTimers = function () {
        window.clearInterval(_countdownTimer);
        window.clearTimeout(_popupTimer);
    }